%macro print_listings(bbb, program, idisname, disname, ds_list, ttt, fr/*, ftnt*/);

%get_data(lst=adpe,pre=adam,supp =Y,lib=adam);

data pe(keep=subjid usubjid param paramcd avisit avisitn avalc /*PECLSIG pedtc petmc result dt*/ srcseq _d adtm dt atm);
*length result $100. /*date $10. time  $5.*/ dt $20.;
	set ADAMADPE;
where avisitn  ^in (-2 , -1);
if	index(avalc,&bbb)>0;
*if	index(avalc,"ABNORMAL")>0;
*if index(avalc,"ABNORMAL");
if ~missing(adt) then pedtc=put(adt,yymmdd10.);
if ~missing(atm) then petmc=put(atm,TOD5.);
*dt=strip(pedtc)||"/"||strip(petmc);
dt =catx(" / ",pedtc,petmc);
/*xx=index(avalc,"(");*/
/*if xx ne 0 then do;*/
/*result=strip(substr(avalc,xx+1));*/
/*result=compress(result,")"," ");*/
/*end;*/
if paramcd="APPEAR" then _d=1; 
else if paramcd="CARDIO" then _d=2; 
else if paramcd="GASTRO" then _d=3;
else if paramcd="HEENT" then _d=4; 
else if paramcd="MUSCULO" then _d=5; 
else if paramcd="NECK" then _d=6;
else if paramcd="NEURO" then _d=7;
else if paramcd="RESP" then _d=8;
else if paramcd="SKIN" then _d=9;
else if paramcd="OTHER" then _d=10;
if paramcd='OTHER' then PARAM=strip(paramcd)||': '||strip(petestot);
	if avisit ne "Screening" then output;
run;
	
proc sort data=pe ;
by _d subjid avisitn dt /*pedtc petmc result*/; 
run;

proc sql;
 select count(distinct subjid)
 into :doit
 from PE;
quit;

%page_cut(in=pe,sort= _d avisitn dt /*pedtc petmc result*/,nocut=avisitn,newpage=,pagesize=35,skip=subjid,order=,
	columns=, width=,defln=1,nlsym=\par,out=pe1);

title3 " ";
title4 "Table &disname (Continued)";
%footer1(foot1=1, ds_list=&ds_list.,program=&program.);

filename filetmp temp;
filename filertf "\\algopharm.com\algorithmepharmadata\Biostudies\Montreal\&prot.\SRA\Biostatistics\Work\Outputs\tables\T&idisname..PE.ABN.&fr.rtf";

%open_rtf;
%empty_ds_fix(pe1);

title5 "Listing of &ttt On-Study Physical Examination";
title6 "(Safety Population)";

*footnote3 j=l "&ftnt";
/*Body System Examined	Subject ID	Visit	Date / Time 		Result (Abnormal Findings)*/
proc report data=pe1 missing split='~' headline headskip spacing=1 nowd;
	column page _d paramcd param subjid avisitn avisit dt avalc;

define page     / " " order order=internal noprint;
define _d       / " " order order=internal noprint;
define paramcd     / " " order order=internal noprint;
define param  / "Body System Examined~ " order order=internal style(column)=[cellwidth=2.1in  just=left] style(header)=[just=left];

define subjid  / "Subject ID~ " order order=internal style(column)=[cellwidth=1.3in  just=left] style(header)=[just=left];

define avisitn     / " " order order=internal noprint;
define avisit   / "Visit~ " order order=internal style(column)=[cellwidth=1.6in  just=left] style(header)=[just=left];

   define dt     / "Date/Time~ " order order=internal style(column)=[cellwidth=1.8in  just=left] style(header)=[just=left];

   define avalc   / "Result (Abnormal Findings)~ "  order=internal style(column)=[cellwidth=3.1in  just=left] style(header)=[just=left] flow;

/*   define peorres   / "Result (Abnormal Finding)"  order=internal style(column)=[cellwidth=2.8in  just=left] style(header)=[just=left] flow;*/

     break after page/page;
 
   compute before _d;
    line " ";
   endcomp;

   compute after avisitn;
    line " ";
   endcomp;

 %if &doit EQ 0 %then %do;
   compute after page;
    line "No &ttt On-Study Physical Examination Values Were Measured.";
   endcomp;
 %end;
  run;

%close_rtf;
%arrange_rtf;

/*%end;*/
%mend print_listings;

%print_listings(bbb=%str("ABNORMAL"),program=T14_03_07_0X_ABN_PE, idisname=14.3.7.1, disname=14.3.7.1, ds_list=ADPE, ttt=Abnormal, fr=ALL. /*, ftnt=%str(NCS: Not Clinically Significant / CS: Clinically Significant)*/ );

proc datasets lib=work kill nolist nodetails; run;

%print_listings(bbb=%str("ABNORMAL CS"),program=T14_03_07_0X_ABN_PE, idisname=14.3.7.2, disname=14.3.7.2, ds_list=ADPE, ttt=Clinically Significant, fr=CS. /*, ftnt=%str(CS: Clinically Significant)*/ );

proc datasets lib=work kill nolist nodetails; run;
